package com.insleton.admin.image.model;

import com.google.common.collect.Sets;
import com.vteba.annotation.Proto;
import io.swagger.v3.oas.annotations.media.Schema;
import com.vteba.utils.common.CaseUtils;
import io.protostuff.Tag;
import java.util.Set;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

import jakarta.persistence.Transient;
import jakarta.persistence.Id;
import jakarta.persistence.TemporalType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Temporal;
import java.util.Date;

/**
 * 图片资源Bean，Protobuf协议用于前端数据封装和输出。
 * 
 * @author tongku2008@126.com
 * @since 2025年6月17日 10:30:55
 */
@Entity
@Proto
@Schema(description = "图片资源对象")
public class ImageResource {

	// private static final long serialVersionUID = 1L;

	/**
	 * 分页开始记录
	 */
	private transient Integer start;
	/**
	 * 内部使用的排序语句
	 */
	private transient String orderClause;
	/**
	 * 所有表字段，用于验证排序字段，不需要的可以删除掉。
	 */
	private static final Set<String> columnSet = Sets.newHashSet("id", "title", "image_url", "states", "comments",
			"tenant_id", "update_date", "remark");
	/**
	 * 是否去重
	 */
	private transient Boolean distinct;

	/** 主键 */
	@Schema(description = "主键", type = "String")
	@Tag(1)
	private String id;
	/** 标题说明 */
	@Schema(description = "标题说明", type = "String")
	@Tag(2)
	private String title;
	/** 图片地址 */
	@Schema(description = "图片地址", type = "String")
	@Tag(3)
	private String imageUrl;
	/** 状态 */
	@Schema(description = "状态", type = "String")
	@Tag(4)
	private String states;
	/** 备注 */
	@Schema(description = "备注", type = "String")
	@Tag(5)
	private String comments;
	/** 机构id */
	@Schema(description = "机构id", type = "String")
	@Tag(6)
	private String tenantId;
	/** 更新时间 */
	@Schema(description = "更新时间", type = "Date")
	@Tag(7)
	private Long updateTimestamp;
	private transient Date updateDate;
	/** 说明 */
	@Schema(description = "说明", type = "String")
	@Tag(8)
	private String remark;
	/** 页码 */
	@Transient
	@Schema(description = "页码", type = "Integer")
	@Tag(9)
	private Integer page;
	/** 分页大小 */
	@Transient
	@Schema(description = "分页大小", type = "Integer")
	@Tag(10)
	private Integer pageSize;
	/** order by 排序语句，格式：name:a;create_date:d。字段名:a/d，不同字段用分号分隔。a标识升序，d标识降序，不存在的字段名将被忽略 */
	@Transient
	@Schema(description = "order by 排序语句", type = "String")
	@Tag(11)
	private String orderBy;
	/** 用于前端排序 */
	@Transient
	@Schema(description = "用于前端排序", type = "Integer")
	@Tag(12)
	private Integer index;
	/** 用于传递多个Id */
	@Transient
	@Schema(description = "用于传递多个Id", type = "List<String>")
	@Tag(13)
	private List<String> idsList;

	public ImageResource() {
	}

	/**
	 * 设置 order by 排序语句
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * 获得 order by 排序语句
	 */
	@Transient
	public String getOrderBy() {
		return orderBy;
	}

	/**
	 * 获得真正的排序语句
	 */
	@Transient
	public String getOrderClause() {
		// 内部直接设置的排序语句，优先级最高
		if (StringUtils.isNotBlank(orderClause)) {
			return orderClause;
		}
		if (StringUtils.isNotBlank(orderBy)) {
			String[] clauseArray = StringUtils.split(orderBy, ";");
			StringBuilder sb = new StringBuilder();
			for (String clause : clauseArray) {
				String[] orderClauses = StringUtils.split(clause, ":");
				if (orderClauses == null || orderClauses.length != 2) {
					continue;
				}
				String column = orderClauses[0];
				String ascDesc = orderClauses[1];

				String direct = null; // 排序方向
				if ("a".equals(ascDesc)) {
					direct = " asc,";
				} else if ("d".equals(ascDesc)) {
					direct = " desc,";
				}

				String orderColumn = CaseUtils.underCase(column);
				// 字段存在，且排序方向是对的，才是一个正确的排序参数
				if (columnSet.contains(orderColumn) && StringUtils.isNotBlank(direct)) {
					sb.append(orderColumn).append(direct);
				}
			}
			int len = sb.length();
			if (len > 0) {
				return sb.deleteCharAt(len - 1).toString();
			}
		}
		return null;
	}

	/**
	 * 设置真正的排序语句，优先级比通过orderBy传入的要高
	 */
	public void setOrderClause(String orderClause) {
		this.orderClause = orderClause;
	}

	/**
	 * 设置 start，分页开始记录
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * 获得 start，分页开始记录
	 */
	@Transient
	public Integer getStart() {
		if (page == null) {
			return null;
		}
		if (start != null) {
			return start;
		} else {
			if (page < 1) {
				page = 1;
			}
			return (page - 1) * pageSize;
		}
	}

	/**
	 * 设置页码，可以根据page计算start
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * 获得页码page
	 */
	@Transient
	public Integer getPage() {
		return page;
	}

	/**
	 * 设置 pageSize，分页大小
	 */
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 获得 pageSize，分页大小
	 */
	@Transient
	public Integer getPageSize() {
		return pageSize;
	}

	/**
	 * 获得 index
	 */
	@Transient
	public Integer getIndex() {
		return index;
	}

	/**
	 * 设置 index
	 */
	public void setIndex(Integer index) {
		this.index = index;
	}

	// @Transient // 防止获取不必要的参数
	// public Set<String> getColumnSet() {
	// 	return columnSet;
	// }

	@Transient
	public List<String> getIdsList() {
		return idsList;
	}

	public void setIdsList(List<String> idsList) {
		this.idsList = idsList;
	}

	/**
	 * 设置 distinct，是否去重
	 */
	public void setDistinct(Boolean distinct) {
		this.distinct = distinct;
	}

	/**
	 * 获得 distinct，是否去重
	 */
	@Transient
	public Boolean isDistinct() {
		return distinct;
	}

	public ImageResource(String id, String title, String imageUrl, String states, String comments, String tenantId,
			Date updateDate, String remark) {
		super();
		this.id = id;
		this.title = title;
		this.imageUrl = imageUrl;
		this.states = states;
		this.comments = comments;
		this.tenantId = tenantId;
		this.updateDate = updateDate;
		this.remark = remark;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false, length = 50)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "title", nullable = false, length = 255)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "image_url", nullable = false, length = 255)
	public String getImageUrl() {
		return this.imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	@Column(name = "states", length = 20)
	public String getStates() {
		return this.states;
	}

	public void setStates(String states) {
		this.states = states;
	}

	@Column(name = "comments", length = 100)
	public String getComments() {
		return this.comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	@Column(name = "tenant_id", length = 50)
	public String getTenantId() {
		return this.tenantId;
	}

	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "update_date", length = 29)
	public Date getUpdateDate() {
		if (updateTimestamp != null && updateTimestamp != 0) {
			return new Date(updateTimestamp);
		}
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
		if (updateDate != null) {
			this.updateTimestamp = updateDate.getTime();
		}
	}

	@Column(name = "remark", length = 100)
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

}